package com.jianan.mis.app.overseastudy;

import static com.jianan.mis.util.ui.calendar.SWTCalendarWrapper.openCalendar;
import static com.jianan.mis.util.ui.table.TableCreator.createSortedTableColumn;
import static com.jianan.mis.util.ui.table.TableCreator.createTableColumn;

import java.text.Collator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.app.basedata.ChooseEmployeeDialog;
import com.jianan.mis.data.cache.CountryCache;
import com.jianan.mis.data.cache.EmployeeCache;
import com.jianan.mis.data.dao.base.PageControl;
import com.jianan.mis.data.domain.Country;
import com.jianan.mis.data.domain.Employee;
import com.jianan.mis.data.domain.OsConsultation;
import com.jianan.mis.data.domain.OsConsultationState;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.service.OsConsultationService;
import com.jianan.mis.sys.IGroupKeys;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.ResourceManager;
import com.jianan.mis.sys.SpringBeanFactory;
import com.jianan.mis.util.StringUtils;
import com.jianan.mis.util.ui.table.AbstractInvertableTableSorter;

@SuppressWarnings("unchecked")
public class OsConsultationArchiveView extends ViewPart {

	private Combo comboState;
	private Text txtConsultTo;
	private Text txtConsultFrom;
	private Combo comboCountry;
	private Combo comboConsultant;
	private Label lblResult;
	private Label lblPage;
	private Button btnSign;
	private Button btnAssign;
	private Button btnUnintentional;
	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			OsConsultation osConsultation = (OsConsultation) element;
			String text = consultations.getColumn(columnIndex).getText();
			if (text.equals(CONSULTATION_ID)) {
				return osConsultation.getId().toString();
			} else if (text.equals(STUDENT_NAME)) {
				return osConsultation.getName();
			} else if (text.equals(COUNTRY_NAME)) {
				return osConsultation.getCountryIntent().getName();
			} else if (text.equals(CONSULTANT)) {
				if (osConsultation.getConsultant() != null)
					return osConsultation.getConsultant().getName();
				else
					return "";
			} else if (text.equals(CREATE_DATE)) {
				return dateFormat.format(osConsultation.getCreateDate());
			} else if (text.equals(UPDATE_DATE)) {
				return dateFormat.format(osConsultation.getUpdateDate());
			} else if (text.equals(STATE)) {
				if(osConsultation.getState().equals(OsConsultationState.UNSIGNED)) {					
					return "未签约";
				} else if(osConsultation.getState().equals(OsConsultationState.AUDITING)) {				
					return "待审核签约";
				} else if(osConsultation.getState().equals(OsConsultationState.UNREASSIGNED)) {				
					return "过期未分配";
				} else if(osConsultation.getState().equals(OsConsultationState.REASSIGNED)) {				
					return "过期已分配";
				} else if(osConsultation.getState().equals(OsConsultationState.UNINTENTIONAL)) {				
					return "无意向";
				} else {
					return "";
				}
			} else {
				return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	
	private TableViewer consultationTableViewer;
	private Table consultations;
	public static final String ID = "com.jianan.mis.app.overseastudy.OsConsultationArchiveView"; //$NON-NLS-1$

	final private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
	final private int PAGE_SIZE = 15;
	private PageControl pageControl;
	private Map conditions;
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		final FillLayout fillLayout = new FillLayout();
		fillLayout.marginHeight = 10;
		fillLayout.marginWidth = 10;
		parent.setLayout(fillLayout);
		
		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1);
		label.setLayoutData(gd_label);
		label.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label.setText("查询条件:");

		final Label label_1 = new Label(container, SWT.NONE);
		final GridData gd_label_1 = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		label_1.setLayoutData(gd_label_1);
		label_1.setText("咨询员:");

		comboConsultant = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboConsultant = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboConsultant.setLayoutData(gd_comboConsultant);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("咨询国家:");

		comboCountry = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboCountry = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboCountry.setLayoutData(gd_comboCountry);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_3.setText("咨询时间从:");

		txtConsultFrom = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		txtConsultFrom.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtConsultFrom, dateFormat);
			}
		});
		final GridData gd_txtConsultFrom = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtConsultFrom.widthHint = 100;
		txtConsultFrom.setLayoutData(gd_txtConsultFrom);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_4.setText("咨询时间至:");

		txtConsultTo = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		txtConsultTo.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtConsultTo, dateFormat);
			}
		});
		final GridData gd_txtConsultTo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtConsultTo.widthHint = 100;
		txtConsultTo.setLayoutData(gd_txtConsultTo);

		final Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_6.setText("咨询档案状态:");

		comboState = new Combo(container, SWT.READ_ONLY);
		comboState.setItems(new String[] {"未签约", "待审核签约", "过期未分配", "过期已分配"});
		final GridData gd_comboState = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboState.setLayoutData(gd_comboState);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Button btnQuery = new Button(container, SWT.NONE);
		btnQuery.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pageControl = new PageControl(PAGE_SIZE);
				buildQueryConditions();
				pageQuery();
			}
		});
		final GridData gd_btnQuery = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnQuery.setLayoutData(gd_btnQuery);
		btnQuery.setText("查询");
		btnQuery.setImage(ImageManager.getImage(IImageKeys.SEARCH));

		final Button btnReset = new Button(container, SWT.NONE);
		btnReset.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				comboConsultant.deselectAll();
				comboCountry.deselectAll();
				comboState.deselectAll();
				txtConsultFrom.setText("");
				txtConsultTo.setText("");
			}
		});
		final GridData gd_btnReset = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnReset.setLayoutData(gd_btnReset);
		btnReset.setText("重置");
		btnReset.setImage(ImageManager.getImage(IImageKeys.RESET));
		
		new Label(container, SWT.NONE);

		final Label label_5 = new Label(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1));
		label_5.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_5.setText("查询结果:");
		
		new Label(container, SWT.NONE);

		new Label(container, SWT.NONE);
		
		lblResult = new Label(container, SWT.NONE);
		lblResult.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		lblResult.setText("");

		lblPage = new Label(container, SWT.NONE);
		lblPage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		lblPage.setText("");

		final Button btnFirstPage = new Button(container, SWT.NONE);
		btnFirstPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageCount() > 0) {
					pageControl.setPageNo(1);
					pageQuery();
				}
			}
		});
		final GridData gd_btnFirstPage = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnFirstPage.setLayoutData(gd_btnFirstPage);
		btnFirstPage.setText("首页");
		btnFirstPage.setImage(ImageManager.getImage(IImageKeys.LEFT));

		final Button btnPrevPage = new Button(container, SWT.NONE);
		btnPrevPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageNo() - 1 > 0) {
					pageControl.setPageNo(pageControl.getPageNo() - 1);
					pageQuery();
				}
			}
		});
		final GridData gd_btnPrevPage = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnPrevPage.setLayoutData(gd_btnPrevPage);
		btnPrevPage.setText("上页");
		btnPrevPage.setImage(ImageManager.getImage(IImageKeys.UP));

		final Button btnNextPage = new Button(container, SWT.NONE);
		btnNextPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageNo() + 1 <= pageControl.getPageCount()) {
					pageControl.setPageNo(pageControl.getPageNo() + 1);
					pageQuery();
				}
			}
		});
		btnNextPage.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		btnNextPage.setText("下页");
		btnNextPage.setImage(ImageManager.getImage(IImageKeys.DOWN));

		final Button btnLastPage = new Button(container, SWT.NONE);
		btnLastPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageCount() > 0) {
					pageControl.setPageNo(pageControl.getPageCount());
					pageQuery();
				}
			}
		});
		btnLastPage.setLayoutData(new GridData());
		btnLastPage.setText("尾页");
		btnLastPage.setImage(ImageManager.getImage(IImageKeys.RIGHT));

		consultationTableViewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
		consultationTableViewer.setLabelProvider(new TableLabelProvider());
		consultationTableViewer.setContentProvider(new ContentProvider());
		consultations = consultationTableViewer.getTable();
		consultations.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if (osConsultation == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsConsultationDialog dialog = new OsConsultationDialog(
							getViewSite().getShell());
					dialog.setOsConsultation(osConsultation);
					if (Session.getInstance().getGroups().contains(IGroupKeys.OSCHIEF)
							|| Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
						// 可修改
						dialog.setReadonly(false);
						if (dialog.open() == Window.OK) {
							pageControl = new PageControl(PAGE_SIZE);
							pageQuery();
						}
					} else {
						// 不可修改
						dialog.setReadonly(true);
						dialog.open();
					}
				}
			}
		});
		consultations.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if ((Session.getInstance().getGroups().contains(IGroupKeys.FIN_DEP)
						|| Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) 
						&& osConsultation.getState().equals(OsConsultationState.AUDITING)) {
					btnSign.setEnabled(true);
				} else {
					btnSign.setEnabled(false);
				}
			}
		});
		consultations.setLinesVisible(true);
		consultations.setHeaderVisible(true);
		final GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
		gd_table.heightHint = 150;
		consultations.setLayoutData(gd_table);

		final Button btnView = new Button(container, SWT.NONE);
		btnView.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if (osConsultation == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsConsultationDialog dialog = new OsConsultationDialog(
							getViewSite().getShell());
					dialog.setOsConsultation(osConsultation);
					if (Session.getInstance().getGroups().contains(IGroupKeys.OSCHIEF)
							|| Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
						// 可修改
						dialog.setReadonly(false);
						if (dialog.open() == Window.OK) {
							pageControl = new PageControl(PAGE_SIZE);
							pageQuery();
						}
					} else {
						// 不可修改
						dialog.setReadonly(true);
						dialog.open();
					}
				}
			}
		});
		final GridData gd_btnView = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnView.setLayoutData(gd_btnView);
		btnView.setText("查看");
		btnView.setImage(ImageManager.getImage(IImageKeys.FORM));

		btnSign = new Button(container, SWT.NONE);
		btnSign.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if (osConsultation == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsSignConfirmDialog dialog = new OsSignConfirmDialog(
							getViewSite().getShell());
					dialog.setOsConsultation(osConsultation);
					if (dialog.open() == Window.OK) {
						pageControl = new PageControl(PAGE_SIZE);
						pageQuery();
					}
				}
			}
		});
		btnSign.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		btnSign.setText("签约");
		btnSign.setImage(ImageManager.getImage(IImageKeys.SIGN));

		btnAssign = new Button(container, SWT.NONE);
		btnAssign.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		btnAssign.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if (osConsultation == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					if(osConsultation.getState().equals(OsConsultationState.UNSIGNED) || 
					   osConsultation.getState().equals(OsConsultationState.SIGNED)) {
						MessageDialog.openInformation(e.display.getActiveShell(),
								IMessageKeys.FAIL, "该咨询档案不可重分配!");
						return;
					}
					ChooseEmployeeDialog dialog = new ChooseEmployeeDialog(
							getViewSite().getShell());
					dialog.setEmployeeGroups(new String[] {
							IGroupKeys.OSCONSULTANT, IGroupKeys.OSCHIEF});
					dialog.open();
					if(dialog.getSelectedEmployee() != null) {
						if(MessageDialog.openConfirm(e.display.getActiveShell(), IMessageKeys.CONFIRM, 
								"确认分配给 " + dialog.getSelectedEmployee().getName() + " 吗?")) {
							osConsultation.setConsultant(dialog.getSelectedEmployee());
							osConsultation.setState(OsConsultationState.REASSIGNED);
							osConsultationService.update(osConsultation);
							pageQuery();
						}
					}
				}
			}
		});
		btnAssign.setText("分配咨询员");
		btnAssign.setImage(ImageManager.getImage(IImageKeys.ASSIGN));

		btnUnintentional = new Button(container, SWT.NONE);
		btnUnintentional.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) consultationTableViewer
						.getSelection();
				OsConsultation osConsultation = (OsConsultation) selection
						.getFirstElement();
				if (osConsultation == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					if(MessageDialog.openConfirm(e.display.getActiveShell(), 
							IMessageKeys.CONFIRM, "确认设置该档案为无意向吗?")) {
						osConsultation.setState(OsConsultationState.UNINTENTIONAL);
						osConsultationService.update(osConsultation);
						pageControl = new PageControl(PAGE_SIZE);
						pageQuery();
					}
				}
			}
		});
		btnUnintentional.setText("设置为无意向");
		btnUnintentional.setImage(ImageManager.getImage(IImageKeys.DELETE));
		
		initializeTableViewer();
		initializeServices();
		setupValues();
		filterPermission();
		notice();
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	public static final String CONSULTATION_ID = "咨询档案号";
	public static final String STUDENT_NAME = "咨询学生";
	public static final String COUNTRY_NAME = "咨询国家";
	public static final String CONSULTANT = "咨询顾问";
	public static final String CREATE_DATE = "咨询表创建时间";
	public static final String UPDATE_DATE = "咨询表最后更新时间";
	public static final String STATE = "状态";
	
	private void initializeTableViewer() {
		/*createSortedTableColumn(consultationTableViewer, SWT.CENTER, CONSULTATION_ID, "", 120,
				new ConsultationIdSorter(), SWT.UP, false, null).chooseColumnForSorting();*/
		createSortedTableColumn(consultationTableViewer, SWT.CENTER, STUDENT_NAME, "", 120,
				new StudentNameSorter(), SWT.UP, false, null).chooseColumnForSorting();
		createSortedTableColumn(consultationTableViewer, SWT.CENTER, COUNTRY_NAME, "", 80,
				new CountryNameSorter(), SWT.UP, false, null);
		createSortedTableColumn(consultationTableViewer, SWT.CENTER, CONSULTANT, "", 120,
				new ConsultantSorter(), SWT.UP, false, null);
		createSortedTableColumn(consultationTableViewer, SWT.CENTER, CREATE_DATE, "", 170,
				new CreateDateSorter(), SWT.UP, false, null);
		createSortedTableColumn(consultationTableViewer, SWT.CENTER, UPDATE_DATE, "", 170,
				new UpdateDateSorter(), SWT.UP, false, null);
		createTableColumn(consultationTableViewer, SWT.CENTER, STATE, "", 100, null);
	}
	
	List<Integer> consultantIds = new ArrayList<Integer>();
	List<Integer> countryIds = new ArrayList<Integer>();
	
	private void setupValues() {
		List<Employee> consultants = EmployeeCache.getInstance().getEmployeesByGroups(
				new String[] { IGroupKeys.OSCONSULTANT, IGroupKeys.OSCHIEF });
		if (consultants != null) {
			for (Employee employee : consultants) {
				consultantIds.add(employee.getId());
				comboConsultant.add(employee.getName());
			}
		}
		List<Country> countries = CountryCache.getInstance().getAllCountries();
		if (countries != null) {
			for (Country country : countries) {
				countryIds.add(country.getId());
				comboCountry.add(country.getName());
			}
		}
	}
	
	private boolean onlyConsultant;
	private void filterPermission() {
		onlyConsultant = false;
		if (Session.getInstance().getGroups().contains(IGroupKeys.OSCHIEF)
				|| Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
			btnAssign.setEnabled(true);
			btnUnintentional.setEnabled(true);
			comboState.add("无意向");
		} else {
			btnAssign.setEnabled(false);
			btnUnintentional.setEnabled(false);
			if(Session.getInstance().getGroups().contains(IGroupKeys.OSCONSULTANT)) {
				onlyConsultant = true;
			}
		}
		if (onlyConsultant) {
			comboConsultant.setEnabled(false);
		}
	}
	
	private void notice() {
		if (Session.getInstance().getGroups().contains(IGroupKeys.OSCHIEF)
				|| Session.getInstance().getGroups().contains(
						IGroupKeys.MANAGER)) {
			List<OsConsultation> consultations = osConsultationService
					.getByStates(new int[] { OsConsultationState.UNREASSIGNED });
			if (consultations.size() > 0) {
				MessageDialog.openInformation(getViewSite().getShell(),
						IMessageKeys.INFORMATION, "有 " + consultations.size()
								+ " 件已过期档案还未重新分配咨询员!");
			}
		}
	}
	
	private void buildQueryConditions() {
		conditions = new HashMap();
		if (onlyConsultant) {
			conditions.put("onlyConsultantId", Session.getInstance().getEmployeeId());
		} else if (comboConsultant.getSelectionIndex() >= 0) {
			conditions.put("consultantId", consultantIds.get(comboConsultant.getSelectionIndex()));
		}
		if (comboCountry.getSelectionIndex() >= 0) {
			conditions.put("countryId", countryIds.get(comboCountry.getSelectionIndex()));
		}
		if (StringUtils.isNotEmpty(txtConsultFrom.getText())) {
			try {
				conditions.put("from", dateFormat.parse(txtConsultFrom.getText()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (StringUtils.isNotEmpty(txtConsultTo.getText())) {
			try {
				conditions.put("to", dateFormat.parse(txtConsultTo.getText()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (comboState.getSelectionIndex() >= 0) {
			conditions.put("state", comboState.getSelectionIndex());
		}
		if (comboState.getItemCount() == 3) {
			conditions.put("restricted", true);
		}
	}
	
	private void pageQuery() {
		osConsultationService.pageQuery(pageControl, conditions);
		consultationTableViewer.setInput(pageControl.getPageData());
		lblResult.setText("查询结果共 " + pageControl.getRowCount() + " 条记录");
		lblPage.setText("页码(每页显示" + PAGE_SIZE + "条): " + pageControl.getPageNo() + "/" + pageControl.getPageCount());
		consultations.deselectAll();
	}

	private OsConsultationService osConsultationService;
	
	private void initializeServices() {
		osConsultationService = (OsConsultationService) SpringBeanFactory.getBean("osConsultationService");
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	// Convenience class which avoids casting in subclasses
	private abstract class AbstractSorter extends AbstractInvertableTableSorter {
		public int compare(Viewer viewer, Object o1, Object o2) {
			return compare((OsConsultation) o1, (OsConsultation) o2);
		}
		public abstract int compare(OsConsultation c1, OsConsultation c2);
	}
	/*private class ConsultationIdSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return NumberUtils.compare(c1.getId(), c2.getId());
		}
	}*/
	private class StudentNameSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return Collator.getInstance().compare(c1.getName(), c2.getName());
		}
	}
	private class CountryNameSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return Collator.getInstance().compare(
					c1.getCountryIntent().getName(),
					c2.getCountryIntent().getName());
		}
	}
	private class ConsultantSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return Collator.getInstance().compare(c1.getConsultant().getName(),
					c2.getConsultant().getName());
		}
	}
	private class CreateDateSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return c1.getCreateDate().compareTo(c2.getCreateDate());
		}
	}
	private class UpdateDateSorter extends AbstractSorter {
		public int compare(OsConsultation c1, OsConsultation c2) {
			return c1.getUpdateDate().compareTo(c2.getUpdateDate());
		}
	}

}
